package com.insigma.business.qrcode.common._bff.impl;


import com.insigma.business.qrcode.common.dto.*;
import com.insigma.business.qrcode.common._consumer.CommonAdminAreaConsumer;
import com.insigma.business.qrcode.common._consumer.CommonAttachmentConsumer;
import com.insigma.business.qrcode.common._bff.CommonBffProvider;
import com.insigma.business.qrcode.common._consumer.SystemConfigConsumer;
import com.insigma.business.qrcode.common.util.ResponseUtils;
import com.insigma.business.qrcode.common.vo.BaseResponse;
import com.insigma.business.qrcode.common.vo.ComonAttachmentRequest;
import com.insigma.business.qrcode.soldier.util.BizCheckUtil;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import org.springframework.stereotype.Service;


@Slf4j
@Service
@Component("commonBffProvider")
public class CommonBffProviderImpl implements CommonBffProvider {


    @Autowired
    private CommonAdminAreaConsumer commonAdminAreaConsumer;

    @Autowired
    private CommonAttachmentConsumer commonAttachmentConsumer;
    @Autowired
    private SystemConfigConsumer systemConfigConsumer;


    @Override
    public BaseResponse<CommonAdminAreaDTO> analyseAreaCodeByBasicInfoForOther(String address) {
        BaseResponse<String> stringBaseResponse = commonAdminAreaConsumer.analyseAreaCodeByBasicInfoForOther(address);

        String areaId = stringBaseResponse.getDto();
        if (areaId != null) {
            CommonAdminAreaDTO adminAreaById = commonAdminAreaConsumer.getAdminAreaById(areaId);
            return ResponseUtils.buildSuccessResponse(adminAreaById);
        }
        return ResponseUtils.buildSuccessResponse(null);
    }

    @Override
    public BaseResponse<String> insertCommonAttachment(ComonAttachmentRequest request) {
        BizCheckUtil.checkNotNull(request, "新增CommonAttachment的请求参数不能为空");
        CommonAttachmentInsertRequest commonAttachmentInsertRequest = new CommonAttachmentInsertRequest();
        BeanUtils.copyProperties(request, commonAttachmentInsertRequest);
        BaseResponse<String> result = commonAttachmentConsumer.insertCommonAttachment(commonAttachmentInsertRequest);
        return result;
    }

    @Override
    public BaseResponse<SystemConfigResult> getSystemConfigByName(String name) {
        BaseResponse<SystemConfigDTO> systemConfigByName = systemConfigConsumer.getSystemConfigByName(name);
        SystemConfigDTO dto = systemConfigByName.getDto();
        if (dto != null) {
            SystemConfigResult result = new SystemConfigResult();
            BeanUtils.copyProperties(dto, result);
            return ResponseUtils.buildSuccessResponse(result);
        }
        return ResponseUtils.buildSuccessResponse(null);
    }


}
